package com.gagan.tplmatcher;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @author gagan.goku@gmail.com
 */
public class Util {
  public static String readFile(String file, boolean appendLineSeparator) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String line;
    StringBuilder stringBuilder = new StringBuilder();
    String ls = System.getProperty("line.separator");
    while((line = reader.readLine()) != null) {
      // Ignore comments.
      if (!line.startsWith("#")) {
        stringBuilder.append(line);
        if (appendLineSeparator) {
          stringBuilder.append(ls);
        }
      }
    }

    return stringBuilder.toString();
  }

  public static void writeRegexsToFiles(List<String> regexs, String baseDir,
                                        String prefix) {
    FileWriter fstream;
    int count = -1;
    for (String r : regexs) {
      count++;
      try {
        fstream = new FileWriter(baseDir + prefix + count + ".txt");
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(r);
        out.close();
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  }
}
